package net.shopxx.action.shop;


import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.jsp.PageContext;

import net.shopxx.entity.Product;
import net.shopxx.entity.ProductComment;
import net.shopxx.service.OrderService;
import net.shopxx.service.ProductCommentService;
import net.shopxx.service.ProductService;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.oscache.base.Cache;
import com.opensymphony.oscache.web.ServletCacheAdministrator;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 前台Action类 - 商品评论
 */

@ParentPackage("shop")
public class ProductcommentAction extends BaseShopAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8982038175671094442L;


	private ProductComment productComment;
	

	@Resource
	private ProductCommentService productCommentService;
	@Resource
	private OrderService orderService;
	@Resource
	private ProductService productService;

	// 列表
	public String list() {
//		商品评价的列表
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ProductComment.class);
		detachedCriteria.add(Restrictions.eq("member.id", getLoginMember().getId()));
		pager = productCommentService.findByPager(pager,detachedCriteria);
		return LIST;
	}
	// 添加
	public String add() {
		return INPUT;
	}
	// 保存
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "productComment.title", message = "标题不允许为空"),
			@RequiredStringValidator(fieldName = "productComment.rank", message = "评分不能为空"),
			@RequiredStringValidator(fieldName = "productComment.experience", message = "使用心得不能为空"),
			@RequiredStringValidator(fieldName = "productComment.advantage", message = "优点不能为空"),
			@RequiredStringValidator(fieldName = "productComment.shortcoming", message = "不足不能为空"),
		},
		stringLengthFields={
			@StringLengthFieldValidator(fieldName="productComment.title",minLength="4",maxLength="20",message="标题长度在4-20个字之间"),
			@StringLengthFieldValidator(fieldName = "productComment.rank",minLength="1",maxLength="1", message = "请正确选择评分"),
			@StringLengthFieldValidator(fieldName = "productComment.experience",minLength="5",maxLength="200", message = "使用心得长度在5-200个字之间"),
			@StringLengthFieldValidator(fieldName = "productComment.advantage",minLength="5",maxLength="100", message = "忧点长度在5-100个字之间"),
			@StringLengthFieldValidator(fieldName = "productComment.shortcoming",minLength="5",maxLength="100", message = "缺点长度在5-100个字之间"),
		}
		
	)
	@InputConfig(resultName = "error")
	public String save() throws Exception {
		Product product = productService.load(productComment.getProduct().getId());
		productComment.setMember(getLoginMember());
		productComment.setProduct(product);
		productCommentService.save(productComment);
		flushCache();
		redirectionUrl = product.getHtmlFilePath();
		return SUCCESS;
	}
	
	
	
	public ProductComment getProductComment() {
		return productComment;
	}
	public void setProductComment(ProductComment productComment) {
		this.productComment = productComment;
	}
	// 更新页面缓存
	private void flushCache() {
		Cache cache = ServletCacheAdministrator.getInstance(getRequest().getSession().getServletContext()).getCache(getRequest(), PageContext.APPLICATION_SCOPE); 
		cache.flushAll(new Date());
	}
	

}